﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GalacticLib
{
    public class DataSetData
    {
        public string GalaxyName { get; set; }
        public string Morphology { get; set; }
        public double glon { get; set; }
        public double glat { get; set; }
        public double z { get; set; }
        public double v { get; set; }
        public double Distance;

        public static double H = 70; // Hubble Constant km/s / mpc
        public static double C = 300000; // //Speed of light 300,000,000m/s

        public long id { get; set; }
        public double glonrad { get; set; }
        public double glatrad { get; set; }
        public int DataSetId { get; set; }

        static string RedshiftUnit = "km/s";

        public double PD_DistanceRad;
        public double PD_LogCount;
        public double PD_LogDistance;

        public static bool TryParse(string line, Regex regexObject, Dictionary<string, int> fieldMapDictionary, out DataSetData dsd)
        {
            dsd = new DataSetData();

            Match m = regexObject.Match(line);

            if (m.Captures.Count > 0)
            {
                //the groups array is indeed zero index, however 0 is the line that was matched.
                dsd.GalaxyName = m.Groups[fieldMapDictionary["GalaxyName"]].Value;
                dsd.Morphology = m.Groups[fieldMapDictionary["Morphology"]].Value;
                dsd.glon = double.Parse(m.Groups[fieldMapDictionary["glon"]].Value);
                dsd.glat = double.Parse(m.Groups[fieldMapDictionary["glat"]].Value);
                dsd.v = double.Parse(m.Groups[fieldMapDictionary["v"]].Value);
                
                double D;

                if (RedshiftUnit == "km/s")
                {
                    dsd.z = dsd.v / C;
                }

                double z = dsd.z;
                double czOverh = (C * z) / H;
                double onePlusZOver2 = 1 + (z / 2);
                double onePlusZSquared = Math.Pow(1 + z, 2);
                
                //D = ((C * dsd.z) / H) * ( ( 1 + dsd.z / 2 ) / Math.Pow(1 + dsd.z, 2) );

                D = czOverh * (onePlusZOver2 / onePlusZSquared);
                
                //D = (C / H) * dsd.z;
               // D = C * dsd.z / H;

                //double v = Math.Pow(dsd.z + 1, 2) - 1;
                //double c = Math.Pow(dsd.z + 1, 2) + 1;
                //double vOverC = v / c;


                //double D = ((C * vOverC) / H) * ( ( 1 + vOverC / 2 ) / Math.Pow(1 + vOverC, 2) );

                dsd.Distance = D;

                return true;
            }
            else
            {
                return false;
            }
        }

        public override string ToString()
        {
            return GalaxyName + "; " + Morphology + "; " + glon + "; " + glat;
        }
    
    }

   
}
